package m202406.d29;

import java.util.Stack;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/6/29 15:34
 */
public class Q739 {
    public int[] dailyTemperatures(int[] temperatures) {
        int[] res = new int[temperatures.length];
        Stack<Element> stack = new Stack<>();
        for (int i = 0; i < temperatures.length; i++) {
            if (stack.isEmpty() || stack.peek().value >= temperatures[i]) {
                stack.push(new Element(i, temperatures[i]));
            } else {
                while (!stack.isEmpty() && stack.peek().value < temperatures[i]) {
                    Element pop = stack.pop();
                    res[pop.index] = i - pop.index;
                }
                stack.push(new Element(i, temperatures[i]));
            }
        }
        return res;
    }

    private static class Element {
        private final int index;
        private final int value;

        public Element(int index, int value) {
            this.index = index;
            this.value = value;
        }
    }
}
